#include "stdio.h"
#include <stdlib.h>
typedef struct node
{
    int data;
    struct node *next;
} node;
node *head = NULL, *ptr, *tail;

void init()
{
    ptr = (node *)malloc(sizeof(node));
    ptr->data = 1;
    head = ptr;
    ptr->next = NULL;

    ptr = (node *)malloc(sizeof(node));
    ptr->data = 2;
    head->next = ptr;
    ptr->next = NULL;

    ptr = (node *)malloc(sizeof(node));
    ptr->data = 3;
    head->next->next = ptr;
    ptr->next = NULL;
    tail = ptr;
}
void Print_AllNode(node *head)
{
    int cnt = 0;
    while (head != NULL)
    {
        printf("链表节点数据依次为:\r\n序号:%d,数据:%d\r\n", cnt, head->data);
        head = head->next;
        cnt++;
    }
}
//返回次序为输入值前面一个的节点
node *Find_Node(node *head)
{
    int z = 0;
    printf("请输入序号:");
    scanf("%d", &z);

    for (int i = 0; i < z-1; i++)
    {
        if (head)
        {
            head = head->next;
        }
        else
        {
            printf("超出索引,最大索引:%d\r\n",i);
            return NULL;
        }
    }
    return head;
}
void Insert(node *ptr)
{
    int fun = 0;
    while (1)
    {
        printf("请输入插入位置:\r\n0.退出\t1.头部插入\t2.中间插入\t3.尾部插入\r\n");
        scanf("%d", &fun);
        if (!fun)
            break;
        ptr = (node *)malloc(sizeof(node));
        printf("请输入数据:\r\n");
        scanf("%d", &(ptr->data));
        switch (fun)
        {
        case 1:
            ptr->next = head;
            head = ptr;
            break;
        case 2:
            node* temp  = Find_Node(head);
            if(temp)
            {
                ptr->next = temp->next;
                temp->next = ptr;
            }
            break;
        case 3:
            tail->next = ptr;
            ptr->next = NULL;
            tail = ptr;
            break;
        default:
            break;
        }
    }
}
int main()
{
    init();
    Insert(head);
    Print_AllNode(head);
}